import { BrowserWindow } from 'electron'
import { createWindow, OptionsType } from './createWindow'
import IpcMainEvent = Electron.IpcMainEvent
import IpcMainInvokeEvent = Electron.IpcMainInvokeEvent

export type WindowNameType = 'start' | 'main' | 'createProject'

export const config: Record<WindowNameType, OptionsType> = {
  start: {
    id: 0,
    title: 'Welcome to Wdhcr',
    width: 500,
    height: 350,
    center: true,
    titleBarStyle: 'default',
    titleBarOverlay: false,
    transparent: true,
    frame: false,
    hash: '/#start'
  },
  main: {
    id: 1,
    width: 1200,
    title: 'Welcome to Wdhcr',
    height: 800,
    center: true,
    frame: true,
    titleBarStyle: 'hidden',
    titleBarOverlay: true,
    hash: '/#/',
    openDevTools: true
  },
  createProject: {
    id: 2,
    title: 'Welcome to WdhcrVite',
    width: 800,
    height: 1000,
    center: true,
    titleBarStyle: 'default',
    titleBarOverlay: false,
    frame: true,
    hash: '/#/create',
    openDevTools: true
  }
}

export const getByNameWindow = (name: WindowNameType) => {
  let win = BrowserWindow.fromId(config[name].id!)
  if (!win) {
    win = createWindow(config[name])
    config[name].id = win.id
  }
  return win
}

export const getWindowByEvent = (event: IpcMainEvent | IpcMainInvokeEvent) => {
  return BrowserWindow.fromWebContents(event.sender)!
}
